home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / git-4.3 / git-4 / git-4.3.11 / src / xmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  2.0 KB  |  91 lines

  1. /* xmalloc.c -- Safe memory management routines.  Includes xmalloc, xcalloc,
  2.    xrealloc and free.  fatal() is called when there is no more memory
  3.    available.  */
  4.  
  5. /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Written by Tudor Hulubei and Andrei Pitis.  */
  22.  
  23.  
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27.  
  28. #include "xmalloc.h"
  29.  
  30.  
  31. void fatal PROTO ((char *));
  32.  
  33.  
  34. char *
  35. xmalloc(size)
  36.     size_t size;
  37. {
  38.     void *pointer = malloc(size ? size : 1);
  39.  
  40.     if (pointer == NULL)
  41.     fatal("xmalloc: virtual memory exhausted");
  42.  
  43.     return (char *)pointer;
  44. }
  45.  
  46.  
  47. char *
  48. xcalloc(count, itemsize)
  49.     size_t count, itemsize;
  50. {
  51.     void *pointer;
  52.  
  53.     if (count && itemsize)
  54.     pointer = calloc(count, itemsize);
  55.     else
  56.     pointer = calloc(1, 1);
  57.  
  58.     if (pointer == NULL)
  59.     fatal("xcalloc: virtual memory exhausted");
  60.  
  61.     return (char *)pointer;
  62. }
  63.  
  64.  
  65. char *
  66. xrealloc(pointer, size)
  67.     void *pointer;
  68.     size_t size;
  69. {
  70.     /* I know realloc should call malloc if 'pointer' is NULL, but it seems
  71.        to work better on suns. Strange... */
  72.     void *new_pointer = pointer ? realloc(pointer, size ? size : 1) :
  73.                   malloc(size ? size : 1);
  74.  
  75.     if (new_pointer == NULL)
  76.     fatal("xrealloc: virtual memory exhausted");
  77.  
  78.     return (char *)new_pointer;
  79. }
  80.  
  81.  
  82. void
  83. xfree(pointer)
  84.     void *pointer;
  85. {
  86.     if (pointer)
  87.     free(pointer);
  88.     else
  89.     fatal("xfree: trying to free NULL pointer");
  90. }
  91.